package core;

import java.awt.Dimension;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;

import javax.swing.ImageIcon;

/**
 * @author Victor Jimenez Kwast
 * @version 1.1
 * @since 2011-04-11
 */
public abstract class Unit {

	protected Point2D.Double position;
	protected int viewAngle; // In degrees
	protected int health;
	protected Dimension size;
	protected ImageIcon image;
	protected Ellipse2D.Double bounds;
	protected final int maxHealth;

	/**
	 * @param position de positie waar je je bevind
	 * @param viewAngle het gebied wat je kan zien
	 * @param health het aantal levens dat je nog over hebt
	 * @param size de grote 
	 * @param image het plaatje wat er bij hoort
	 */
	/* /// Constructor //////////////////////////////////////// */
	public Unit(Point2D.Double position, int health, ImageIcon image, Dimension size, int viewAngle) {
		this.position = position;
		this.viewAngle = viewAngle;
		this.health = health;
		this.size = size;
		this.image = image;
		this.maxHealth = health;

		if (position != null)
			calcBounds();
	}

	/* /// calcBounds //////////////////////////////////////// */
	public void calcBounds() {
		this.bounds = new Ellipse2D.Double(position.x, position.y, getSize().getWidth(), getSize().getHeight());
	}

	/* /// getHealth //////////////////////////////////////// */
	public int getHealth() {
		return health;
	}

	/* /// setHealth //////////////////////////////////////// */
	public void setHealth(int health) {
		this.health = health;
	}

	/* /// getImage //////////////////////////////////////// */
	public ImageIcon getImage() {
		return image;
	}

	/* /// getPosition //////////////////////////////////////// */
	public Point2D.Double getPosition() {
		return position;
	}

	/* /// setPosition //////////////////////////////////////// */
	public void setPosition(Point2D.Double position) {
		this.position = position;

		calcBounds();
	}

	/* /// getSize //////////////////////////////////////// */
	public Dimension getSize() {
		return size;
	}

	/* /// getViewAngle //////////////////////////////////////// */
	public int getViewAngle() {
		return viewAngle;
	}

	/* /// setViewAngle //////////////////////////////////////// */
	public void setViewAngle(int viewAngle) {
		this.viewAngle = viewAngle;
	}

	/* /// update //////////////////////////////////////// */
	public boolean update() {
		calcBounds();
		return false;
	}

	/* /// getBounds //////////////////////////////////////// */
	public Ellipse2D.Double getBounds() {
		return this.bounds;
	}

	/* /// getMiddle //////////////////////////////////////// */
	public Point2D.Double getMiddle() {
		return new Point2D.Double(this.position.x + getSize().getWidth() / 2, this.position.y + getSize().getHeight()
				/ 2);
	}

	/**
	 * Controleert of de unit niet meer het scherm te zien is (buiten het scherm
	 * valt)
	 * 
	 * @return boolean
	 */
	/* /// isOutsideView //////////////////////////////////////// */
	public boolean isOutsideView() {
		int minPosX = 0 - getSize().width / 2;
		int minPosY = 0 - getSize().height / 2;

		int maxPosX = Model.battlefieldWidth + getSize().width / 2;
		int maxPosY = Model.battlefieldHeight + getSize().height / 2;

		return (this.position.x < minPosX || this.position.y < minPosY | this.position.x > maxPosX || this.position.y > maxPosY);
	}

	/* /// getMaxHealth //////////////////////////////////////// */
	public int getMaxHealth() {
		return maxHealth;
	}

	/* /// getHealthPercentage //////////////////////////////////////// */
	public double getHealthPercentage() {
		return (double) health / (double) maxHealth;
	}
}
